package com.ecable.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ecable.bean.VillageTO;
import com.ecable.delegate.VillageMgrDelegate;
import com.ecable.exception.ConnectionException;
import com.ecable.exception.DataNotFoundException;
import com.ecable.util.UtilConstants;

public class ViewRegVillageAction extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7790811550795184033L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String path="";
		
		
		Vector<VillageTO> vvto=null;
		
		try{
			
			try{
				
				
				
				vvto=new VillageMgrDelegate().viewVillage();
				
			}
			catch (ConnectionException e) {
		   throw new ServletException("ConnectionTO Failed");
		   
			}
			catch(DataNotFoundException e){
				
				throw new ServletException("No Data found");
				}
			
			if(!vvto.isEmpty()){
				
				request.setAttribute("status", UtilConstants._ALL_VILLAGES);
				
				request.setAttribute("villageVector", vvto);
				
				path=UtilConstants._DISPLAY_VILLAGE_HOME;
				
			}
			else{
				
				request.setAttribute("status", UtilConstants._ALL_VILLAGE_FAILED);
				
				path=UtilConstants._DISPLAY_VILLAGE_HOME;
				
			}
			
			
	}
		catch (Exception e) {
			e.printStackTrace();
			
			request.setAttribute("status", UtilConstants._INVALIED_ENTRY);
			
			path=UtilConstants._DISPLAY_VILLAGE_HOME;
			
		}
		
		
		RequestDispatcher rd=request.getRequestDispatcher(path);
		
		rd.forward(request, response);
	}

}
